﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;

namespace Human
{
	/// <summary>
	/// Summary description for frmHopdong.
	/// </summary>
	public class frmdmHopdong : System.Windows.Forms.Form
	{
        private AccessData mDB = new AccessData();
        Language lan = new Language();
        DataSet dshd = new DataSet();
        string user = "";
		private System.Windows.Forms.TextBox txt_ma;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txt_ten;
		private System.Windows.Forms.Button btn_them;
		private System.Windows.Forms.Button btn_luu;
		private System.Windows.Forms.Button btn_sua;
		private System.Windows.Forms.Button btn_thoat;
		private System.Windows.Forms.Button btn_xoa;
		DataSet ds =new DataSet();
		private System.Windows.Forms.Button btn_boqua;
        private ComboBox cbo_nhombc;
        private Label label3;
        string sql = "";
        private Label label4;
        private TextBox txt_stt;
        private DataGridView dgrv_hopdong;
        private Label label5;
        private TextBox txt_sothang;
        private DataGridViewTextBoxColumn mahd;
        private DataGridViewTextBoxColumn tenhd;
        private DataGridViewTextBoxColumn tennhom;
        private DataGridViewTextBoxColumn sothang;
        private DataGridViewTextBoxColumn stt;
        private DataGridViewTextBoxColumn manhom;
		private System.ComponentModel.Container components = null;

		public frmdmHopdong()
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmdmHopdong));
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            this.txt_ma = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.txt_ten = new System.Windows.Forms.TextBox();
            this.btn_them = new System.Windows.Forms.Button();
            this.btn_luu = new System.Windows.Forms.Button();
            this.btn_sua = new System.Windows.Forms.Button();
            this.btn_thoat = new System.Windows.Forms.Button();
            this.btn_xoa = new System.Windows.Forms.Button();
            this.btn_boqua = new System.Windows.Forms.Button();
            this.cbo_nhombc = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.txt_stt = new System.Windows.Forms.TextBox();
            this.dgrv_hopdong = new System.Windows.Forms.DataGridView();
            this.mahd = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.tenhd = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.tennhom = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.sothang = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.stt = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.manhom = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.label5 = new System.Windows.Forms.Label();
            this.txt_sothang = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.dgrv_hopdong)).BeginInit();
            this.SuspendLayout();
            // 
            // txt_ma
            // 
            this.txt_ma.BackColor = System.Drawing.SystemColors.HighlightText;
            this.txt_ma.ForeColor = System.Drawing.Color.Navy;
            this.txt_ma.Location = new System.Drawing.Point(7, 283);
            this.txt_ma.MaxLength = 2;
            this.txt_ma.Name = "txt_ma";
            this.txt_ma.Size = new System.Drawing.Size(53, 20);
            this.txt_ma.TabIndex = 1;
            this.txt_ma.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_ma_KeyDown);
            // 
            // label1
            // 
            this.label1.ForeColor = System.Drawing.Color.Navy;
            this.label1.Location = new System.Drawing.Point(4, 265);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 15);
            this.label1.TabIndex = 2;
            this.label1.Text = "Mã HĐ ";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label2
            // 
            this.label2.ForeColor = System.Drawing.Color.Navy;
            this.label2.Location = new System.Drawing.Point(63, 265);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(46, 15);
            this.label2.TabIndex = 4;
            this.label2.Text = "Tên HĐ";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txt_ten
            // 
            this.txt_ten.BackColor = System.Drawing.SystemColors.HighlightText;
            this.txt_ten.ForeColor = System.Drawing.Color.Navy;
            this.txt_ten.Location = new System.Drawing.Point(66, 283);
            this.txt_ten.MaxLength = 40;
            this.txt_ten.Name = "txt_ten";
            this.txt_ten.Size = new System.Drawing.Size(114, 20);
            this.txt_ten.TabIndex = 2;
            this.txt_ten.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_ten_KeyDown);
            // 
            // btn_them
            // 
            this.btn_them.BackColor = System.Drawing.SystemColors.Control;
            this.btn_them.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_them.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_them.Image = global::Human.Properties.Resources.add;
            this.btn_them.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_them.Location = new System.Drawing.Point(40, 307);
            this.btn_them.Name = "btn_them";
            this.btn_them.Size = new System.Drawing.Size(60, 28);
            this.btn_them.TabIndex = 5;
            this.btn_them.Text = "&Thêm";
            this.btn_them.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_them.UseVisualStyleBackColor = false;
            this.btn_them.Click += new System.EventHandler(this.btn_them_Click);
            this.btn_them.KeyDown += new System.Windows.Forms.KeyEventHandler(this.btthem_KeyDown);
            // 
            // btn_luu
            // 
            this.btn_luu.BackColor = System.Drawing.SystemColors.Control;
            this.btn_luu.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_luu.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_luu.Image = ((System.Drawing.Image)(resources.GetObject("btn_luu.Image")));
            this.btn_luu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_luu.Location = new System.Drawing.Point(233, 307);
            this.btn_luu.Name = "btn_luu";
            this.btn_luu.Size = new System.Drawing.Size(60, 28);
            this.btn_luu.TabIndex = 6;
            this.btn_luu.Text = "   &Lưu";
            this.btn_luu.UseVisualStyleBackColor = false;
            this.btn_luu.Click += new System.EventHandler(this.btn_luu_Click);
            // 
            // btn_sua
            // 
            this.btn_sua.BackColor = System.Drawing.SystemColors.Control;
            this.btn_sua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_sua.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_sua.Image = ((System.Drawing.Image)(resources.GetObject("btn_sua.Image")));
            this.btn_sua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_sua.Location = new System.Drawing.Point(104, 307);
            this.btn_sua.Name = "btn_sua";
            this.btn_sua.Size = new System.Drawing.Size(60, 28);
            this.btn_sua.TabIndex = 7;
            this.btn_sua.Text = "   &Sửa";
            this.btn_sua.UseVisualStyleBackColor = false;
            this.btn_sua.Click += new System.EventHandler(this.btn_sua_Click);
            // 
            // btn_thoat
            // 
            this.btn_thoat.BackColor = System.Drawing.SystemColors.Control;
            this.btn_thoat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_thoat.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_thoat.Image = global::Human.Properties.Resources.close_r;
            this.btn_thoat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_thoat.Location = new System.Drawing.Point(370, 307);
            this.btn_thoat.Name = "btn_thoat";
            this.btn_thoat.Size = new System.Drawing.Size(60, 28);
            this.btn_thoat.TabIndex = 10;
            this.btn_thoat.Text = "&Thoát";
            this.btn_thoat.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_thoat.UseVisualStyleBackColor = false;
            this.btn_thoat.Click += new System.EventHandler(this.btn_thoat_Click);
            // 
            // btn_xoa
            // 
            this.btn_xoa.BackColor = System.Drawing.SystemColors.Control;
            this.btn_xoa.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_xoa.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_xoa.Image = global::Human.Properties.Resources.delete_enabled;
            this.btn_xoa.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_xoa.Location = new System.Drawing.Point(168, 307);
            this.btn_xoa.Name = "btn_xoa";
            this.btn_xoa.Size = new System.Drawing.Size(60, 28);
            this.btn_xoa.TabIndex = 9;
            this.btn_xoa.Text = "    &Xóa";
            this.btn_xoa.UseVisualStyleBackColor = false;
            this.btn_xoa.Click += new System.EventHandler(this.btn_xoa_Click);
            // 
            // btn_boqua
            // 
            this.btn_boqua.BackColor = System.Drawing.SystemColors.Control;
            this.btn_boqua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btn_boqua.ForeColor = System.Drawing.Color.DarkBlue;
            this.btn_boqua.Image = ((System.Drawing.Image)(resources.GetObject("btn_boqua.Image")));
            this.btn_boqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btn_boqua.Location = new System.Drawing.Point(296, 307);
            this.btn_boqua.Name = "btn_boqua";
            this.btn_boqua.Size = new System.Drawing.Size(70, 28);
            this.btn_boqua.TabIndex = 8;
            this.btn_boqua.Text = "&Bỏ qua";
            this.btn_boqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btn_boqua.UseVisualStyleBackColor = false;
            this.btn_boqua.Click += new System.EventHandler(this.btn_boqua_Click);
            // 
            // cbo_nhombc
            // 
            this.cbo_nhombc.BackColor = System.Drawing.SystemColors.HighlightText;
            this.cbo_nhombc.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbo_nhombc.Enabled = false;
            this.cbo_nhombc.FormattingEnabled = true;
            this.cbo_nhombc.Location = new System.Drawing.Point(186, 283);
            this.cbo_nhombc.Name = "cbo_nhombc";
            this.cbo_nhombc.Size = new System.Drawing.Size(162, 21);
            this.cbo_nhombc.TabIndex = 3;
            this.cbo_nhombc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cbo_nhombc_KeyDown);
            // 
            // label3
            // 
            this.label3.ForeColor = System.Drawing.Color.Navy;
            this.label3.Location = new System.Drawing.Point(183, 265);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(38, 13);
            this.label3.TabIndex = 10;
            this.label3.Text = "Nhóm ";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label4
            // 
            this.label4.ForeColor = System.Drawing.Color.Navy;
            this.label4.Location = new System.Drawing.Point(425, 265);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(53, 14);
            this.label4.TabIndex = 10;
            this.label4.Text = "STT";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // txt_stt
            // 
            this.txt_stt.Location = new System.Drawing.Point(428, 284);
            this.txt_stt.Name = "txt_stt";
            this.txt_stt.Size = new System.Drawing.Size(50, 20);
            this.txt_stt.TabIndex = 5;
            this.txt_stt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_stt_KeyDown_1);
            // 
            // dgrv_hopdong
            // 
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            this.dgrv_hopdong.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
            this.dgrv_hopdong.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgrv_hopdong.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.mahd,
            this.tenhd,
            this.tennhom,
            this.sothang,
            this.stt,
            this.manhom});
            this.dgrv_hopdong.Location = new System.Drawing.Point(0, 1);
            this.dgrv_hopdong.Name = "dgrv_hopdong";
            this.dgrv_hopdong.RowHeadersVisible = false;
            this.dgrv_hopdong.Size = new System.Drawing.Size(491, 247);
            this.dgrv_hopdong.TabIndex = 11;
            this.dgrv_hopdong.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgrv_hopdong_CellContentClick);
            // 
            // mahd
            // 
            this.mahd.DataPropertyName = "mahd";
            this.mahd.HeaderText = "Mã HĐ";
            this.mahd.Name = "mahd";
            this.mahd.Width = 50;
            // 
            // tenhd
            // 
            this.tenhd.DataPropertyName = "tenhd";
            this.tenhd.HeaderText = "Tên hợp đồng";
            this.tenhd.Name = "tenhd";
            this.tenhd.Width = 180;
            // 
            // tennhom
            // 
            this.tennhom.DataPropertyName = "tennhom";
            this.tennhom.HeaderText = "Nhóm";
            this.tennhom.Name = "tennhom";
            this.tennhom.Width = 150;
            // 
            // sothang
            // 
            this.sothang.DataPropertyName = "sothang";
            this.sothang.HeaderText = "Số tháng";
            this.sothang.Name = "sothang";
            this.sothang.Width = 60;
            // 
            // stt
            // 
            this.stt.DataPropertyName = "stt";
            this.stt.HeaderText = "Số TT";
            this.stt.Name = "stt";
            this.stt.Width = 50;
            // 
            // manhom
            // 
            this.manhom.DataPropertyName = "nhombc";
            this.manhom.HeaderText = "Mã nhóm";
            this.manhom.Name = "manhom";
            // 
            // label5
            // 
            this.label5.ForeColor = System.Drawing.Color.Navy;
            this.label5.Location = new System.Drawing.Point(352, 264);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(67, 14);
            this.label5.TabIndex = 10;
            this.label5.Text = "Số tháng";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // txt_sothang
            // 
            this.txt_sothang.Location = new System.Drawing.Point(355, 283);
            this.txt_sothang.Name = "txt_sothang";
            this.txt_sothang.Size = new System.Drawing.Size(51, 20);
            this.txt_sothang.TabIndex = 4;
            this.txt_sothang.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_sothang_KeyDown);
            // 
            // frmdmHopdong
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(491, 337);
            this.Controls.Add(this.dgrv_hopdong);
            this.Controls.Add(this.txt_sothang);
            this.Controls.Add(this.txt_stt);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.cbo_nhombc);
            this.Controls.Add(this.btn_boqua);
            this.Controls.Add(this.btn_thoat);
            this.Controls.Add(this.btn_xoa);
            this.Controls.Add(this.btn_sua);
            this.Controls.Add(this.btn_luu);
            this.Controls.Add(this.btn_them);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.txt_ten);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txt_ma);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmdmHopdong";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Danh mục hợp đồng";
            this.Load += new System.EventHandler(this.frmHopdong_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dgrv_hopdong)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

        private void frmHopdong_Load(object sender, System.EventArgs e)
        {
            user = mDB.user;
            load_nhom();
            load_dgrv();
            ena_ctrl(false);
            ena_btn(true);
            btn_them.Focus();
        }
        private void load_nhom()
        {            
            cbo_nhombc.ValueMember = "manhom";
            cbo_nhombc.DisplayMember = "tennhom";
            cbo_nhombc.DataSource = mDB.get_nhombc().Tables[0];
            cbo_nhombc.SelectedIndex = -1;
        }
        private void load_dgrv()
        {
            dshd = mDB.get_data("select mahd,tenhd,nhombc,sothang,stt,nb.tennhom from "+user+".mahd mh left join "+user+".nhombc nb on mh.nhombc=nb.manhom order by mh.nhombc");
            dgrv_hopdong.DataSource = dshd.Tables[0];
        }
        private void ena_ctrl(bool ena)
        {
            txt_ma.Enabled = ena;
            txt_ten.Enabled = ena;
            cbo_nhombc.Enabled = ena;
            txt_sothang.Enabled = ena;
            txt_stt.Enabled = ena;
        }
        private void ena_btn(bool ena)
        {
            btn_them.Enabled = ena;
            btn_sua.Enabled = !ena;
            btn_luu.Enabled = !ena;
            btn_boqua.Enabled = !ena;
            btn_xoa.Enabled = !ena;
            btn_thoat.Enabled = ena;
        }
        private void clear_ctrl()
        {
            txt_ma.Text = "";
            txt_ten.Text = "";
            cbo_nhombc.SelectedIndex = -1;
            txt_sothang.Text = "0";
            txt_stt.Text = "0";
        }
        private bool kemtra(string mahd)
        {
            bool flag = false;
            string t = mDB.get_data("select count(*) as tong from " + mDB.user + ".hopdong where loaihd='" + mahd + "' ").Tables[0].Rows[0][0].ToString();
            try
            {
                if (Convert.ToInt32(t) > 0)
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
            }
            catch
            {
                flag = false;
            }
            if (flag)
                MessageBox.Show(lan.Change_language_MessageText("Loại hợp đồng đã sử dụng không thể hủy!"),AccessData.Msg);
            return flag;
        }	
		private void btthem_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode ==Keys.Enter)
				txt_ma.Focus();
		}       

        private void dgrv_hopdong_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataRowView drv = (DataRowView)dgrv_hopdong.CurrentRow.DataBoundItem;

            txt_ma.Text = drv["mahd"].ToString();
            txt_ten.Text = drv["tenhd"].ToString();
            cbo_nhombc.SelectedValue = drv["nhombc"].ToString().Trim().Equals("") ? cbo_nhombc.SelectedIndex = -1 : cbo_nhombc.SelectedValue = drv["nhombc"].ToString().Trim();
            txt_sothang.Text = drv["sothang"].ToString();
            txt_stt.Text = drv["stt"].ToString();
            ena_ctrl(false);
            ena_btn(false);
            btn_luu.Enabled = false;
            btn_thoat.Enabled = true;
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            ena_ctrl(true);
            clear_ctrl();
            ena_btn(false);
            btn_xoa.Enabled = false;
            btn_sua.Enabled = false;
            txt_ma.Focus();
        }

        private void btn_sua_Click(object sender, EventArgs e)
        {
            ena_ctrl(true);
            txt_ma.Enabled = false;
            ena_btn(false);
            btn_sua.Enabled = false;
            btn_xoa.Enabled = false;
            txt_ten.Focus();
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show(lan.Change_language_MessageText("Bạn có muốn hủy thông tin hợp đồng?"), AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes && !kemtra(txt_ma.Text.Trim()))
                {
                    if (txt_ma.Text.Trim() != "")
                        mDB.Execute_data("DELETE  FROM " + mDB.user + ".mahd WHERE mahd='" + txt_ma.Text.Trim() + "'");
                }
            }
            catch { }
            load_dgrv();
            clear_ctrl();
            ena_ctrl(false);
            ena_btn(true);
            btn_them.Focus();
        }

        private void btn_luu_Click(object sender, EventArgs e)
        {
            string ma = "", ten = "", nhom = "", sql = "";
            int stt=0,sothang=0;
            if (txt_ma.Text.Trim().ToUpper() == "")
            {
                MessageBox.Show(lan.Change_language_MessageText("Nhập mã"), AccessData.Msg);
                txt_ma.Focus(); return;
            }
            if (txt_ten.Text.Trim() == "")
            {
                MessageBox.Show(lan.Change_language_MessageText("Nhập tên hợp đồng"), AccessData.Msg);
                txt_ten.Focus(); return;
            }
            if (cbo_nhombc.SelectedIndex == -1)
            {
                MessageBox.Show(lan.Change_language_MessageText("Nhập nhóm báo cáo"), AccessData.Msg);
                cbo_nhombc.Focus(); return;
            }
            else
            {
                nhom = cbo_nhombc.SelectedValue.ToString();
            }
            if (txt_stt.Text != "")
            {
                stt = Int32.Parse(txt_stt.Text);
            }
            else
            {
                stt = 0;
            }
            if (txt_sothang.Text != "")
            {
                sothang = Int32.Parse(txt_sothang.Text);
            }
            else
            {
                sothang = 0;
            }
            ma = txt_ma.Text.Trim().ToUpper();
            ten = txt_ten.Text.Trim();

            if (mDB.get_data("select * from " + mDB.user + ".mahd where mahd='" + txt_ma.Text.Trim() + "'").Tables[0].Rows.Count >= 1)
            {
                sql = "update " + mDB.user + ".mahd set tenhd ='" + ten + "',";
                sql += " nhombc=" + nhom + ",stt=" + stt.ToString() + ",sothang="+sothang.ToString()+" ";
                sql += " where mahd='" + txt_ma.Text.Trim().ToUpper() + "'";
                mDB.Execute_data(sql);
            }
            else
            {
                sql = "INSERT INTO " + mDB.user + ".mahd(mahd,tenhd,nhombc,stt,sothang) VALUES ('" + ma.Trim() + "','" + ten.Trim() + "'," + nhom + "," + stt.ToString() + ","+sothang.ToString()+" )";
                mDB.Execute_data(sql);
            }
            load_dgrv();
            ena_ctrl(false);
            ena_btn(true);
            btn_them.Focus();		
        }

        private void btn_boqua_Click(object sender, EventArgs e)
        {
            ena_ctrl(false);            
            ena_btn(true);
            btn_them.Focus();
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_ma_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txt_ten_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}{F4}");
        }

        private void cbo_nhombc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txt_sothang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }
        private void txt_stt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn_luu.Focus();
        }

        private void txt_stt_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btn_luu.Focus();
        }
	}
}
